- All important points from the training course, collected for quick reference. See also [[Tutorial]]s. - ### SPARQL Interactions collapsed:: true - ![SPARQLinteractions.svg](../assets/SPARQLinteractions_1643113721643_0.svg){:height 304, :width 746} - See [[SPARQL Editors]] - ### Two triple patterns, same subject collapsed:: true - ```sparql SELECT ?country ?capital ?population WHERE { ?country dbo:capital ?capital . ?country dbp:populationTotal ?population . } ``` #Query #DBpedia - ![TwoPatternsSameSubject.svg](../assets/TwoPatternsSameSubject_1643114435930_0.svg){:height 279, :width 522} - ### Making another step in the graph collapsed:: true - ```sparql SELECT ?country ?population ?capital WHERE { ?country a dbo:Country . ?country dbp:populationTotal ?population . ?country dbo:capital ?capital_URI . ?capital_URI rdfs:label ?capital . } ``` #Query #DBpedia - ![AnotherStepInTheGraph.svg](../assets/AnotherStepInTheGraph_1643114924911_0.svg){:height 405, :width 671} - collapsed:: true ```sparql PREFIX skos: PREFIX skosxl: SELECT * WHERE { ?s skosxl:prefLabel ?Label_URI . ?Label_URI skosxl:literalForm ?label . FILTER (LANG(?label) = "es") } ``` - #VocBench #Query - ### Three patterns and two filters collapsed:: true - ```sparql SELECT ?country ?population ?capital WHERE { ?country a dbo:Country; dbo:populationTotal ?population; dbo:capital ?capital_URI . ?capital_URI rdfs:label ?capital . FILTER ( LANG(?capital) = "en" ) FILTER ( xsd:int(?population) > 10000000 ) } ORDER BY DESC(?population) ``` #Query #DBpedia #FILTER #LANG - ### Counting and Binding collapsed:: true - ```sparql SELECT (COUNT (?work_of_Agatha_Christie) AS ?Number_of_AC_works) {?work_of_Agatha_Christie dbo:author dbr:Agatha_Christie } ``` #Query #COUNT - ### Functions on dates and times collapsed:: true - [[NOW]]() - [[YEAR]]() - [[MONTH]]() - [[DAY]]() - [[HOURS]]() - [[MINUTES]]() - [[SECONDS]]() - [[Example]] collapsed:: true - ```sparql SELECT (MONTH(NOW()) AS ?currentMonth) { } ``` #Query - ### Filtering resources with the same namespace collapsed:: true - Find all [[SKOS]] properties in use collapsed:: true - ```sparql SELECT DISTINCT ?property WHERE { ?s ?property ?o . FILTER (STRSTARTS(STR(?property), "http://www.w3.org/2004/02/skos/core#" )) } ``` #Query #STRSTARTS - ### Negation collapsed:: true - `NOT EXISTS` - Testing for absence of a pattern (recommended in most cases) - `MINUS` - Removing possible solutions - `!BOUND` - Not bound (produces the same results as `NOT EXISTS` but works also for SPARQL 1.0) - #+BEGIN_IMPORTANT `NOT EXISTS` and `MINUS` have different logic and may produce different results on the same dataset. #+END_IMPORTANT - ```sparql SELECT DISTINCT ?leader WHERE { ?MS a dbo:Country; dct:subject dbc:Member_states_of_the_European_Union; dbp:leaderName ?leader . FILTER NOT EXISTS {?leader dbo:spouse ?spouse .} } ORDER BY DESC(?leader) ``` #Query #FILTER #[[NOT EXISTS]] - ### Optional results collapsed:: true - ```sparql SELECT DISTINCT ?leader ?spouse WHERE { ?MS a dbo:Country; dct:subject dbc:Member_states_of_the_European_Union; dbp:leaderName ?leader . OPTIONAL {?leader dbo:spouse ?spouse .} } ``` #Query #OPTIONAL - ### Combining results collapsed:: true - ```sparql SELECT DISTINCT ?Influencer ?Influenced WHERE { {dbr:Ludwig_Wittgenstein dbo:influencedBy ?Influencer .} UNION {dbr:Ludwig_Wittgenstein dbo:influenced ?Influenced . } } ``` #Query #UNION - #+BEGIN_NOTE This query will not give complete results, unless inferred triples on inverse properties are included. To guarantee complete results relying only on SPARQL, the inverse property should be included. #+END_NOTE - ### Search for string patterns id:: 61fd3a3d-81b5-448d-a76f-d1b0008b7476 collapsed:: true - ```sparql SELECT DISTINCT ?MusicalWork ?Artist WHERE { ?MusicalWorkURI a dbo:MusicalWork ; rdfs:label ?MusicalWork ; dbo:artist ?ArtistURI . ?ArtistURI rdfs:label ?Artist . FILTER (lang(?MusicalWork) = "en") FILTER (lang(?Artist) = "en") FILTER REGEX (?MusicalWork, "broken", "i") } ORDER BY ?MusicalWork ``` #Query #REGEX #FILTER #LANG - ### Assembling strings collapsed:: true - ```sparql PREFIX cdm: SELECT ?reg (IRI(CONCAT(STR(?eli),"/eng/pdf")) AS ?pdf_URL) { ?reg cdm:resource_legal_eli ?eli . } LIMIT 1000 ``` #Query #IRI #CONCAT #Cellar - ### Property paths collapsed:: true - [[Syntax]] collapsed:: true - ```sparql #Inverse Path: ^:property #Inverse path (object to subject) #Sequence Path: :property1/:property2 #A sequence path of :property1 followed by :property2 #Alternative Path: :property1|:property2 #:property1 or property2 (all possibilities are tried) #Zero Or More: :property* #A path that connects the subject and object of the path by zero or more matches of :property #One Or More: :property+ #A path that connects the subject and object of the path by one or more matches of :property #Zero Or One Path: :property? #A path that connects the subject and object of the path by zero or one matches of :property ``` - [Property paths from the SPARQL 1.1 spec](https://www.w3.org/TR/sparql11-query/#propertypaths) - [[Example]] collapsed:: true - ```sparql SELECT (COUNT (DISTINCT ?child) AS ?Number_of_children) { ?MS a dbo:Country; dct:subject dbc:Member_states_of_the_European_Union ; dbp:leaderName ?leader . ?leader dbo:child|^dbo:parent ?child. #Works the same as # {?leader dbo:child ?child.} # UNION # {?child dbo:parent ?leader} } ``` #Query #| #^ #UNION - ![PropertyPath_ChildParent.svg](../assets/PropertyPath_ChildParent_1645774371298_0.svg) - ```sparql SELECT (COUNT (DISTINCT ?Influencer) AS ?Num_Influencer) (COUNT (DISTINCT ?Influenced) AS ?Num_Influenced) WHERE { {dbr:Ludwig_Wittgenstein dbo:influencedBy|^dbo:influenced ?Influencer .} UNION {dbr:Ludwig_Wittgenstein dbo:influenced|^dbo:influencedBy ?Influenced .} } ``` #Query #DBpedia #| #^ #COUNT #UNION - - ### Language tag collapsed:: true - The current best practice for language tags is defined in [RFC5646](https://www.rfc-editor.org/rfc/rfc5646.txt). - [[LANGMATCHES]] collapsed:: true - `FILTER (LANG(?label) = "fr")` will only return results where the language tag of `?label` is `@fr`, while `FILTER LANGMATCHES( LANG(?label), "fr" )` will also include those with regions such as `@fr-BE`. - ### Wikibase data mode collapsed:: true - ![image.png](../assets/image_1646977222963_0.png) - ### Wikidata label service collapsed:: true - Adding "Label" to a variable will get the label of the respective item if the Wikidata Label service is called in the query. For example, for ?item, ?itemLabel will get the labels in the selected language. - ```sparql SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q146 . SERVICE wikibase:label { bd:serviceParam wikibase:language “en" } } ``` #Query #Wikidata - #+BEGIN_TIP To include a call to the Label Service in your query, just press Ctrl-Space and start typing "label" until you see the query fragment. Then change the language if needed. Check the [Label service manual](https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual#Label_service) for other capabilities. #+END_TIP - ### Wikidata Language and visualization service collapsed:: true - Influence graph in the age of Enlightenment #Wikidata #Query #FILTER #&& - ### Properties for statements (Wikidata) id:: 600748be-0c7a-4b4c-807d-a5c41f8ee80e collapsed:: true - Item - Statement - Value - Qualifier Value collapsed:: true - ![WikidataStatementPropertiesGeneric.svg](../assets/WikidataStatementPropertiesGeneric_1644054801681_0.svg) - [[Example]]: US Presidents collapsed:: true - - ![WikidataStatementPropertiesUSpresident.svg](../assets/WikidataStatementPropertiesUSpresident_1644055097874_0.svg){:height 502, :width 680} - With `?statement` variable #Query #Wikidata #Qualifiers - With blank nodes #Query #Wikidata #Qualifiers - See also ((61fe46bb-6fc5-4d5e-b917-d0da5b02a96a)) - ### Providing list of input VALUES collapsed:: true - ```sparql # Example with a block of values, where the third block in the list has no value for ?v2 VALUES (?v1 ?v2) {(:c11 :c21) (:c21 :c22) (:c21 UNDEF)} # When value for only one variable are provided, () can be omitted VALUES ?v1 {:c1 :c2 :c3 :cN} ``` #VALUES - ### Grouping results with GROUP_CONCAT collapsed:: true - ```sparql (GROUP_CONCAT (?V; SEPARATOR = ", ") AS ?groupV) ``` #GROUP_CONCAT - ### Federated queries collapsed:: true - ![FederatedQueries.svg](../assets/FederatedQueries_1647882270796_0.svg)